package com.h3bpm.web.controller;

import OThinker.H3.Controller.ControllerBase;
import OThinker.H3.Entity.Data.Attachment.Attachment;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.h3bpm.web.utils.HttpClientUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/Portal/OnlineFile")
public class OnlineFileReadController extends ControllerBase {

    private static final Logger logger = LoggerFactory.getLogger(OnlineFileReadController.class);

    @Value("${desWeb.url}")
    private String des_web_url;


    @RequestMapping("Read")
    public Object Read(String AttachmentID,String BizObjectSchemaCode,String BizObjectID) {

        logger.info("..........进入移动端在线预览处理");
        JSONObject result = new JSONObject();
        if (this.getUserValidator() == null) {
            result.put("result", -1);
            result.put("message", "用户未登陆");
            return result;
        }

        try {

            Attachment attachment = this.getEngine().getBizObjectManager().GetAttachment(this.getUserValidator().getUserID(),
                    BizObjectSchemaCode, BizObjectID, AttachmentID);

            if (attachment == null) {
                result.put("result", -1);
                result.put("message", "文档获取失败");
                return result;
            }

            Map<String, String> map = new HashMap<>();

            map.put("convertType",getConvertType(attachment.getFileName()));
            map.put("wmContent", this.getUserValidator().getUserName() +"  " + this.getUserValidator().getUserCode());
//        map.put("wmFont","");
            map.put("wmColor","11");
            map.put("wmSize","18");
            map.put("htmlName",attachment.getFileName());

            JSON json = HttpClientUtils.HttpMultipartFormdata(attachment.getContent(), map, des_web_url, attachment.getFileName());

            return json;
        } catch (Exception e) {
            logger.info(e.getMessage(), e);
            result.put("result", -1);
            result.put("message", "系统异常");
            return result;
        }
    }

    private String  office = "doc,docx,xls,xlsx,dot,dotx,ppt,pptx,wps";
    private String  picture = "png,jpg,jpeg";
    private String getConvertType(String fileName){
        String hz = fileName.substring(fileName.lastIndexOf(".")+1);

        logger.info(".....getConvertType={}", hz);
        if (office.indexOf(hz.toLowerCase())!=-1) {
            return "1";
        } else if ("pdf".equals(hz.toLowerCase())) {
            return "14";
        } else if (picture.indexOf(hz.toLowerCase()) != -1) {
            return "23";
        }



        return "1";
    }


    @Override
    public String getFunctionCode() {
        return null;
    }
}
